package com.lc.afbx.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiSJRZDetails;
import com.lc.afbx.conn.GetApiShangJiaRuZhu;
import com.lc.afbx.fragment.FragmentBusinessJoinDetailConnect;
import com.lc.afbx.fragment.FragmentBusinessJoinDetailIntruduce;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/6/3.
 */
public class BusinessJoinDetailActivity extends BaseActivity implements View.OnClickListener {

    private TextView business_join_detail_introduce_tv;
    private TextView business_join_detail_connect_tv;

    private GetApiShangJiaRuZhu.Data data;

    private FragmentBusinessJoinDetailIntruduce fragmentBusinessJoinDetailIntruduce;
    private FragmentBusinessJoinDetailConnect fragmentBusinessJoinDetailConnect;

    private Fragment[] fragments;

    private int index, prePosition;

    private GetApiSJRZDetails getApiSJRZDetails = new GetApiSJRZDetails(new AsyCallBack<GetApiSJRZDetails.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiSJRZDetails.Info info) throws Exception {


            Log.e("这是webView的地址:", info.data.web);

            ((ImageView) findViewById(R.id.business_join_detail_iv)).setImageURI(Uri.parse(info.data.picurl));

            ((TextView) findViewById(R.id.business_join_detail_name_tv)).setText(info.data.title);

            fragmentBusinessJoinDetailIntruduce = (FragmentBusinessJoinDetailIntruduce) FragmentBusinessJoinDetailIntruduce.newInstance(info.data.web);
            fragmentBusinessJoinDetailConnect = (FragmentBusinessJoinDetailConnect) FragmentBusinessJoinDetailConnect.newInstance(data.id);

            fragments = new Fragment[]{fragmentBusinessJoinDetailIntruduce, fragmentBusinessJoinDetailConnect};

            getSupportFragmentManager().beginTransaction().add(R.id.fragment_businessjoin_detail_container_rl, fragmentBusinessJoinDetailIntruduce)
                    .show(fragmentBusinessJoinDetailIntruduce).commit();


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(BusinessJoinDetailActivity.this, toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_join_detail);

        business_join_detail_introduce_tv = ((TextView) findViewById(R.id.business_join_detail_introduce_tv));
        business_join_detail_connect_tv = ((TextView) findViewById(R.id.business_join_detail_connect_tv));

        //上个页面穿过来的
        data = (GetApiShangJiaRuZhu.Data) getIntent().getExtras().getSerializable("data");

        getApiSJRZDetails.id = data.id;
        getApiSJRZDetails.tid = PrefrenceConfig.ONE;
        getApiSJRZDetails.execute(this);

        business_join_detail_introduce_tv.setOnClickListener(this);
        business_join_detail_connect_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.business_join_detail_introduce_tv:

                index = 0;

                if (index != prePosition) {

                    business_join_detail_introduce_tv.setBackgroundColor(getResources().getColor(R.color.jianjie));
                    business_join_detail_introduce_tv.setTextColor(getResources().getColor(R.color.white));

                    business_join_detail_connect_tv.setBackgroundColor(getResources().getColor(R.color.white));
                    business_join_detail_connect_tv.setTextColor(getResources().getColor(R.color.lianxi));

                    trx.hide(fragments[prePosition]);

                    if (!fragments[index].isAdded()) {

                        trx.add(R.id.fragment_businessjoin_detail_container_rl, fragments[index]);
                    }

                    trx.show(fragments[index]).commit();

                    prePosition = index;

                }

                return;

            case R.id.business_join_detail_connect_tv:

                index = 1;

                if (index != prePosition) {

                    business_join_detail_connect_tv.setBackgroundColor(getResources().getColor(R.color.jianjie));
                    business_join_detail_connect_tv.setTextColor(getResources().getColor(R.color.white));

                    business_join_detail_introduce_tv.setBackgroundColor(getResources().getColor(R.color.white));
                    business_join_detail_introduce_tv.setTextColor(getResources().getColor(R.color.lianxi));

                    trx.hide(fragments[prePosition]);

                    if (!fragments[index].isAdded()) {

                        trx.add(R.id.fragment_businessjoin_detail_container_rl, fragments[index]);
                    }

                    trx.show(fragments[index]).commit();

                    prePosition = index;

                }

                return;


        }

    }

}
